package com.appkubes.mall.restful.application.pay.strategy.wxpay.model;


import lombok.Data;
import org.apache.commons.io.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;


@Data
public class WxNaturePayConfig extends WXPayConfig {

    private String wxKey;

    private String wxAppId;

    private String wxMchId;

    private String tradeType;

    private String notify;

    private byte[] certData;

    public WxNaturePayConfig(Map<String, String> map) throws IOException {
        //注释证书 无退款类业务
        InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cert/nandu/nandu_cert20191213.p12");
        this.certData = IOUtils.toByteArray(certStream);
        this.setWxAppId(map.get("appId"));
        this.setWxMchId(map.get("mchId"));
        this.setWxKey(map.get("wxKey"));
        this.setTradeType(map.get("tradeType"));
        this.setNotify(map.get("notify"));

        //certStream.close();
    }

    @Override
    public String getAppID() {
        return this.getWxAppId();
    }

    @Override
    public String getMchID() {
        return this.getWxMchId();
    }

    @Override
    public String getKey() {
        return this.getWxKey();
    }

    @Override
    public InputStream getCertStream() {
        return new ByteArrayInputStream(this.certData);
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 0;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 0;
    }

    @Override
    public IWXPayDomain getWXPayDomain() { // 这个方法需要这样实现, 否则无法正常初始化WXPay
        IWXPayDomain iwxPayDomain = new IWXPayDomain() {
            @Override
            public void report(String domain, long elapsedTimeMillis, Exception ex) {

            }

            @Override
            public DomainInfo getDomain(WXPayConfig config) {
                return new DomainInfo(WXPayConstants.DOMAIN_API, true);
            }
        };
        return iwxPayDomain;
    }
}